package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.MessageCenter;
import com.ruoyi.business.domain.OperationRemind;
import com.ruoyi.business.domain.WxUser;
import com.ruoyi.business.domain.vo.AuthBindingInfoReviewVO;
import com.ruoyi.business.service.IMessageCenterService;
import com.ruoyi.business.service.IOperationRemindService;
import com.ruoyi.business.service.IWxUserService;
import com.ruoyi.business.domain.vo.FamilyMemberVO;
import com.ruoyi.business.vo.OperationRemindType;
import com.ruoyi.business.weapp.CommonContants;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AuthBindingInfoMapper;
import com.ruoyi.business.domain.AuthBindingInfo;
import com.ruoyi.business.service.IAuthBindingInfoService;

/**
 * 业主认证Service业务层处理
 * 
 * @author asher
 * @date 2021-11-11
 */
@Service
public class AuthBindingInfoServiceImpl implements IAuthBindingInfoService 
{
    @Autowired
    private AuthBindingInfoMapper authBindingInfoMapper;

    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private IMessageCenterService messageCenterService;

    @Autowired
    private IOperationRemindService operationRemindService;

    /**
     * 查询业主认证
     * 
     * @param bindingId 业主认证主键
     * @return 业主认证
     */
    @Override
    public AuthBindingInfo selectAuthBindingInfoByBindingId(Long bindingId)
    {
        return authBindingInfoMapper.selectAuthBindingInfoByBindingId(bindingId);
    }

    /**
     * 查询业主认证列表
     * 
     * @param authBindingInfo 业主认证
     * @return 业主认证
     */
    @Override
    public List<AuthBindingInfo> selectAuthBindingInfoList(AuthBindingInfo authBindingInfo)
    {
        return authBindingInfoMapper.selectAuthBindingInfoList(authBindingInfo);
    }

    /**
     * 新增业主认证
     * 
     * @param authBindingInfo 业主认证
     * @return 结果
     */
    @Override
    public int insertAuthBindingInfo(AuthBindingInfo authBindingInfo)
    {
        authBindingInfo.setCreateTime(DateUtils.getNowDate());
        return authBindingInfoMapper.insertAuthBindingInfo(authBindingInfo);
    }

    /**
     * 修改业主认证
     * 
     * @param authBindingInfo 业主认证
     * @return 结果
     */
    @Override
    public int updateAuthBindingInfo(AuthBindingInfo authBindingInfo)
    {
        authBindingInfo.setUpdateTime(DateUtils.getNowDate());
        return authBindingInfoMapper.updateAuthBindingInfo(authBindingInfo);
    }

    /**
     * 批量删除业主认证
     * 
     * @param bindingIds 需要删除的业主认证主键
     * @return 结果
     */
    @Override
    public int deleteAuthBindingInfoByBindingIds(Long[] bindingIds)
    {
        return authBindingInfoMapper.deleteAuthBindingInfoByBindingIds(bindingIds);
    }

    /**
     * 删除业主认证信息
     * 
     * @param bindingId 业主认证主键
     * @return 结果
     */
    @Override
    public int deleteAuthBindingInfoByBindingId(Long bindingId)
    {
        return authBindingInfoMapper.deleteAuthBindingInfoByBindingId(bindingId);
    }

    @Override
    public int applyAuthBindingInfo(AuthBindingInfo authBindingInfo) {
        WxUser wxUser = wxUserService.selectWxUserByUserId(authBindingInfo.getWxUserId());
        if (wxUser == null) {
            return 0;
        }
        authBindingInfo.setStatus(CommonContants.AUTHING);
        authBindingInfo.setUpdateTime(DateUtils.getNowDate());
        int i = insertAuthBindingInfo(authBindingInfo);
        wxUser.setOwnerAuthStatus(CommonContants.AUTHING);
        wxUser.setOwnerAuthStatusDes("业主认证中");
        wxUser.setBindingVillageId(authBindingInfo.getVillageId());
        wxUser.setBindingVillageName(authBindingInfo.getVillageName());
        int updateWxUser = wxUserService.updateWxUser(wxUser);
        return i & updateWxUser;
    }

    @Override
    public int reviewAuthBindingInfo(AuthBindingInfoReviewVO authBindingInfoReviewVO) {

        WxUser wxUser = wxUserService.selectWxUserByUserId(authBindingInfoReviewVO.getWxUserId());
        if (wxUser == null) {
            return 0;
        }

        AuthBindingInfo authBindingInfo = authBindingInfoMapper.selectAuthBindingInfoByBindingId(authBindingInfoReviewVO.getBindingId());
        if (authBindingInfo == null) {
            return 0;
        }
        authBindingInfo.setUpdateTime(DateUtils.getNowDate());
        authBindingInfo.setStatus(authBindingInfoReviewVO.getStatus());
        authBindingInfo.setRemark(authBindingInfoReviewVO.getContent());

        MessageCenter messageCenter = new MessageCenter();
        messageCenter.setTitle("业主认证审核结果");
        String content = "审核通过,请重新进入小程序刷新状态!";
        if (authBindingInfoReviewVO.getStatus().equals("3")) {
            content = "审核被驳回,驳回理由：" + authBindingInfoReviewVO.getContent();
        } else if (authBindingInfoReviewVO.getStatus().equals("4")) {
            content = "认证已被废弃,废弃理由：" + authBindingInfoReviewVO.getContent();
        }

        OperationRemind reflash_login_inf = new OperationRemind("REFLASH_LOGIN_INFO",
                "[业主认证]" + content, authBindingInfo.getBindingId(), "0",
                wxUser.getUserId(), null, DateUtils.getNowDate(), wxUser.getUserId().toString());
        operationRemindService.insertOperationRemind(reflash_login_inf);

        messageCenter.setContent("[业主认证]" + content);
        messageCenter.setIsRead("0");
        messageCenter.setModule("业主认证");
        messageCenter.setDestUserId(authBindingInfoReviewVO.getWxUserId().toString());
        messageCenterService.insertMessageCenter(messageCenter);

        authBindingInfoMapper.updateAuthBindingInfo(authBindingInfo);

        List<AuthBindingInfo> authBindingInfos = authBindingInfoMapper.selectAuthBindingInfoByWxUserId(wxUser.getUserId());

        String status = "0";
        for (AuthBindingInfo item : authBindingInfos) {
            if (item.getStatus().equals("2")) {
                status = "2";
                break;
            }
            if (item.getStatus().equals("1")) {
                status = "1";
                continue;
            }
        }

        wxUser.setStatus(status);
        wxUser.setOwnerAuthStatus(status);
        wxUser.setOwnerAuthStatusDes(CommonContants.STATUS_DES_MAP.get(status));

        return wxUserService.updateWxUser(wxUser);
    }

    @Override
    public List<AuthBindingInfo> selectAuthBindingInfoByWxUserId(Long wxUserId) {
        return authBindingInfoMapper.selectAuthBindingInfoByWxUserId(wxUserId);
    }

    @Override
    public List<AuthBindingInfo> selectAuthBindingInfoByWxUserIdAndStatus(Long wxUserId, String status) {
        return authBindingInfoMapper.selectAuthBindingInfoByWxUserIdAndStatus(wxUserId, status);
    }

    @Override
    public List<FamilyMemberVO> getFamilyMembersByHouseId(Long houseId) {
        return authBindingInfoMapper.getFamilyMembersByHouseId(houseId);
    }

    @Override
    public AuthBindingInfo selectAuthBindingInfoByWxUserIdAndBindingVillageId(Long wxUserId, Long bindingVillageId) {
        return authBindingInfoMapper.selectAuthBindingInfoByWxUserIdAndBindingVillageId(wxUserId, bindingVillageId);
    }

    @Override
    public List<AuthBindingInfo> selectExistAuthBindingInfoByWxUserIdAndVillageId(Long wxUserId, Long villageId) {
        return authBindingInfoMapper.selectExistAuthBindingInfoByWxUserIdAndVillageId(wxUserId, villageId);
    }
}
